package com.ruoyi.hotSpot.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.Date2Utils;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.hotSpot.domain.A2DynamicsInfo;
import com.ruoyi.hotSpot.domain.dto.A2DynamicsInfoCondition;
import com.ruoyi.hotSpot.domain.dto.Coordinate;
import com.ruoyi.hotSpot.mapper.A2DynamicsInfoMapper;
import com.ruoyi.hotSpot.service.IA2DynamicsInfoService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * AIS/ADS-B动态信息Service业务层处理
 *
 * @author ruoyi
 * @date 2025-02-14
 */
@Service
public class A2DynamicsInfoServiceImpl extends ServiceImpl<A2DynamicsInfoMapper, A2DynamicsInfo> implements IA2DynamicsInfoService
{
    @Autowired
    private A2DynamicsInfoMapper a2DynamicsInfoMapper;

    /**
     * 查询AIS/ADS-B动态信息
     *
     * @param id AIS/ADS-B动态信息主键
     * @return AIS/ADS-B动态信息
     */
    @Override
    public A2DynamicsInfo selectA2DynamicsInfoById(Long id)
    {
        return a2DynamicsInfoMapper.selectA2DynamicsInfoById(id);
    }

    /**
     * 查询AIS/ADS-B动态信息列表
     *
     * @param a2DynamicsInfo AIS/ADS-B动态信息
     * @return AIS/ADS-B动态信息
     */
    @Override
    public List<A2DynamicsInfo> selectA2DynamicsInfoList(A2DynamicsInfo a2DynamicsInfo)
    {
        return a2DynamicsInfoMapper.selectA2DynamicsInfoList(a2DynamicsInfo);
    }

    /**
     * 新增AIS/ADS-B动态信息
     *
     * @param a2DynamicsInfo AIS/ADS-B动态信息
     * @return 结果
     */
    @Override
    public int insertA2DynamicsInfo(A2DynamicsInfo a2DynamicsInfo)
    {
        a2DynamicsInfo.setCreateTime(DateUtils.getNowDate());
        return a2DynamicsInfoMapper.insertA2DynamicsInfo(a2DynamicsInfo);
    }

    /**
     * 修改AIS/ADS-B动态信息
     *
     * @param a2DynamicsInfo AIS/ADS-B动态信息
     * @return 结果
     */
    @Override
    public int updateA2DynamicsInfo(A2DynamicsInfo a2DynamicsInfo)
    {
        a2DynamicsInfo.setUpdateTime(DateUtils.getNowDate());
        return a2DynamicsInfoMapper.updateA2DynamicsInfo(a2DynamicsInfo);
    }

    /**
     * 批量删除AIS/ADS-B动态信息
     *
     * @param ids 需要删除的AIS/ADS-B动态信息主键
     * @return 结果
     */
    @Override
    public int deleteA2DynamicsInfoByIds(Long[] ids)
    {
        return a2DynamicsInfoMapper.deleteA2DynamicsInfoByIds(ids);
    }

    /**
     * 删除AIS/ADS-B动态信息信息
     *
     * @param id AIS/ADS-B动态信息主键
     * @return 结果
     */
    @Override
    public int deleteA2DynamicsInfoById(Long id)
    {
        return a2DynamicsInfoMapper.deleteA2DynamicsInfoById(id);
    }

    @Override
    public A2DynamicsInfo getLast(String mmsiIcao) {
        A2DynamicsInfoCondition condition = new A2DynamicsInfoCondition();
        condition.setTargetNum(mmsiIcao);
        QueryWrapper<A2DynamicsInfo> wrapper = this.getQueryWrapper(condition);
        wrapper.orderByDesc("id");
        List<A2DynamicsInfo> list = list(wrapper);
        if(!CollectionUtils.isEmpty(list)){
            return list.get(0);
        }
        return null;
    }

    @Override
    public TableDataInfo page(A2DynamicsInfoCondition condition) {
        IPage<A2DynamicsInfo> page = this.getPage(condition);
        TableDataInfo tableDataInfo = new TableDataInfo();
        tableDataInfo.setRows(page.getRecords());
        tableDataInfo.setTotal(page.getTotal());
        return tableDataInfo;
    }

    @Override
    public List<A2DynamicsInfo> a2DynamicsInfoHistory(A2DynamicsInfoCondition condition) {
        //获取最近10个点位
        IPage<A2DynamicsInfo> page = this.getPage(condition);
        List<A2DynamicsInfo> list = page.getRecords();
        //按baseTime正序排序
        list = list.stream().sorted(Comparator.comparing(A2DynamicsInfo::getBaseTime)).collect(Collectors.toList());
        return list;
    }

    @Override
    public List<A2DynamicsInfo> getList(A2DynamicsInfoCondition condition) {
        QueryWrapper<A2DynamicsInfo> wrapper = this.getQueryWrapper(condition);
        return list(wrapper);
    }


    @Override
    public A2DynamicsInfo getByUniqueCombination(String mmsiIcao, String baseTime, Double lon, Double lat) {
        A2DynamicsInfoCondition condition = new A2DynamicsInfoCondition();
        condition.setTargetNum(mmsiIcao);
        condition.setBaseTime(baseTime);
        condition.setLon(lon);
        condition.setLat(lat);
        List<A2DynamicsInfo> list = this.getList(condition);
        if(!CollectionUtils.isEmpty(list)){
            return list.get(0);
        }
        return null;
    }

    private QueryWrapper<A2DynamicsInfo> getQueryWrapper(A2DynamicsInfoCondition condition){
        QueryWrapper<A2DynamicsInfo> wrapper = new QueryWrapper<>();
        if(StringUtils.isNotBlank(condition.getStartTime())){
            wrapper.ge("base_time", Date2Utils.strToDateTime(condition.getStartTime()));
        }
        if(StringUtils.isNotBlank(condition.getEndTime())){
            wrapper.le("base_time", Date2Utils.strToDateTime(condition.getEndTime()));
        }
        if(condition.getTargetNum()!=null){
            wrapper.eq("mmsi_icao", condition.getTargetNum());
        }
        if(!CollectionUtils.isEmpty(condition.getSpaceArea())){
            StringBuilder sql = new StringBuilder("ST_Within(geom,ST_SetSRID(ST_MakePolygon(ST_GeomFromText('LINESTRING(");
            //查询在空间区域内的坐标点
            List<Coordinate> spaceArea = condition.getSpaceArea();
            int size = spaceArea.size();
            for (int i = 0; i <size ; i++) {
                Coordinate c = spaceArea.get(i);
                sql.append(c.getLon()).append(c.getLat());
                if(i<size-1){
                    sql.append(",");
                }
            }
            sql.append(")')),4326))");
            wrapper.apply(sql.toString());
        }
        if(condition.getType()!=null){
            wrapper.eq("type", condition.getType());
        }
        if(condition.getStatus()!=null){
            wrapper.eq("status", condition.getStatus());
        }
        if(StringUtils.isNotBlank(condition.getBaseTime())){
            wrapper.eq("base_time", Date2Utils.strToDateTime(condition.getBaseTime()));
        }
        if(condition.getLon()!=null){
            wrapper.eq("lon", condition.getLon());
        }
        if(condition.getLat()!=null){
            wrapper.eq("lat", condition.getLat());
        }
        if(condition.getReadStatus()!=null){
            wrapper.eq("read_status", condition.getReadStatus());
        }
        return wrapper;
    }

    private IPage<A2DynamicsInfo> getPage(A2DynamicsInfoCondition condition){
        QueryWrapper<A2DynamicsInfo> wrapper = this.getQueryWrapper(condition);
        wrapper.orderByDesc("id");
        IPage<A2DynamicsInfo> page = page(new Page<>(condition.getPageNum(), condition.getPageSize()), wrapper);
        return page;
    }
}
